package com.lazy.book.service.system.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lazy.book.mapper.system.SysEmailMapper;
import com.lazy.book.pojo.system.pojo.dto.sysemail.SysEmailCreateDTO;
import com.lazy.book.pojo.system.pojo.dto.sysemail.SysEmailListDTO;
import com.lazy.book.pojo.system.pojo.dto.sysemail.SysEmailUpdateDTO;
import com.lazy.book.pojo.system.pojo.po.SysEmail;
import com.lazy.book.pojo.system.pojo.vo.SysEmailVO;
import com.lazy.book.service.system.SysEmailService;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.PageUtils;
import com.sz.core.util.Utils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.lazy.book.pojo.system.pojo.po.table.SysEmailTableDef.SYS_EMAIL;

/**
 * @author lay
 * @since 2025-09-05
 */
@Service
@RequiredArgsConstructor
public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> implements SysEmailService {

    @Override
    public void create(SysEmailCreateDTO dto) {
        SysEmail sysEmail = BeanCopyUtils.copy(dto, SysEmail.class);
        save(sysEmail);
    }

    @Override
    public void update(SysEmailUpdateDTO dto) {
        SysEmail sysEmail = BeanCopyUtils.copy(dto, SysEmail.class);
        updateById(sysEmail);
    }

    @Override
    public PageResult<SysEmailVO> page(SysEmailListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_EMAIL);
        if (Utils.isNotBlank(dto.getSubject())) {
            wrapper.and(SYS_EMAIL.SUBJECT.like(dto.getSubject()));
        }
        Page<SysEmail> page = page(dto.buildPage(), wrapper);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<SysEmailVO> list(SysEmailListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_EMAIL);
        if (Utils.isNotBlank(dto.getSubject())) {
            wrapper.and(SYS_EMAIL.SUBJECT.like(dto.getSubject()));
        }
        return list(wrapper).stream().map(sysEmail -> BeanCopyUtils.copy(sysEmail, SysEmailVO.class)).collect(Collectors.toList());
    }

    @Override
    public void remove(SelectIdsDTO dto) {
        CommonResponseEnum.INVALID_ID.assertIsFalse(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public SysEmailVO detail(Object id) {
        SysEmail sysEmail = getById(id);
        return BeanCopyUtils.copy(sysEmail, SysEmailVO.class);
    }

    @Override
    public void importExcel(ImportExcelDTO dto) {

    }

    @Override
    public void exportExcel(SysEmailListDTO dto, HttpServletResponse response) {

    }
}